<?php

namespace Home\Controller;

use Think\Controller;

class AccountController extends Controller {

    private $model = null;

    function __construct() {
        parent::__construct();
        $this->model = D('Account');
    }

    public function reg() {
        if (IS_POST && IS_AJAX) {
            $data = array(
                'email' => I('post.email', '', 'trim'),
                'nick' => I('post.first_name', '', 'trim'),
                'name' => I('post.last_name', '', 'trim'),
                'mobile' => I('post.mobile_no', '', 'trim'),
                'qq' => I('post.QQ', '', 'trim'),
                'add_time' => time()
            );
            $password = I('post.password', '', 'md5');
            $confirm_password = I('post.confirm_password', '', 'md5');
            if ($password != $confirm_password) {
                $this->error('两次密码输入不一致！');
            }
            if ($this->model->checkAccount($data['email']) > 0) {
                $this->error('此邮箱已注册!');
            }
            $data['password'] = md5($data['email'] . I('post.password', '', 'trim'));
            $result = $this->model->add($data);
            if ($result) {
                $data['id'] = $result;
                session('userinfo', $data);
                $this->success('注册成功！');
            } else {
                $this->error('注册失败！');
            }
        } else {
            $this->display();
        }
    }

    function login() {
        if (IS_POST && IS_AJAX) {
            $email = I('post.userEmail', '', 'trim');
            $password = I('post.pwd', '', 'trim');
            $captcha = I('post.vcode', '', 'trim');
            if (!check_verify($captcha)) {
                $this->error('验证码错误！');
            }
            $userinfo = $this->model->checkLogin($email, $password);
            if (!isset($userinfo['id'])) {
                $this->error('用户名或密码错误！');
            }
            session('userinfo', $userinfo);
            $this->success('登陆成功！');
        } else {
            $this->display();
        }
    }

    function RetrievePwd() {
        if (IS_POST && IS_AJAX) {
            
        } else {
            $this->display();
        }
    }

    function PwdSendEmail() {
        if (IS_POST && IS_AJAX) {
            $email = I('post.email', '', 'trim');
            if (!is_email($email)) {
                $this->error('邮箱格式错误！');
            }
            if ($this->model->checkAccount($email) < 1) {
                $this->error('发送邮件失败,邮箱不存在！');
            }
            $code = encrypt(time() . '||' . $email, E);
            $url = getDomain() . U('account/PasswordReset') . '?code=' . $code;
            $title = '全球smt测评MliEasy找回密码操作';
            $content = '全球smt测评MliEasy找回密码操作，为了您账号的安全，请点在30分钟内击下面链接修改密码!链接只能使用一次，请尽快修改！<br/><a href="' . $url . '" target="_blank">点击修改密码</a>';
            if (sendMail($email, $title, $content)) {
                $this->success('发送邮件成功！');
            }
        }
    }

    function PasswordReset() {
        if (IS_POST && IS_AJAX) {
            list($time, $email) = explode('||', encrypt(I('post.vCode', '', 'trim'), D));
            $pwd = I('post.pwd', '', 'trim');
            if ($time != '' && $email != '' && (time() - $time) < 1800) {
                $data['password'] = md5($email . $pwd);
                $result = $this->model->modifyPassword($email, $data);
                if ($result !== false) {
                    $this->success('修改成功！');
                } else {
                    $this->error('修改失败！');
                }
            } else {
                $this->error('修改失败！');
            }
        } else {
            $status = 0;
            list($time, $email) = explode('||', encrypt(I('get.code', '', 'trim'), D));
            if ($time != '' && $email != '' && (time() - $time) < 1800) {
                $status = 1;
            }
            $this->assign('email', $email);
            $this->assign('status', $status);
            $this->display();
        }
    }

    /**
     * 验证码
     */
    public function verify() {
        ob_clean();
        $Verify = new \Think\Verify();
        $Verify->codeSet = '2345678ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnprstuvwxyz'; // 设置验证码字符为纯数字   
        $Verify->length = 4;
        $Verify->useNoise = true;
        $Verify->useCurve = true;
        $Verify->fontttf = "4.ttf";
        //$Verify->bg = array(196, 223, 246);
        $Verify->entry();
    }

    function out() {
        session('userinfo', null);
        $this->redirect('/Account/login');
    }

}
